﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Lidgren.Network;

namespace Managers {
    public static class NetworkSessionManager {
        public static event EventHandler<EventArgs> OnDataReceived;
        private static NetClient client;
        public static NetClient Client { get { return client; } }

        public static void Create() {
            NetPeerConfiguration netconfiguration = new NetPeerConfiguration("DUST");
            netconfiguration.EnableMessageType(NetIncomingMessageType.DiscoveryResponse);
            client = new NetClient(netconfiguration);
            client.Start();
        }
        public static void Initialize() {
            client.DiscoverLocalPeers(12345);
        }
        public static void Shutdown() {
            if (client != null)
                client.Shutdown("Bye");
        }

        /// <summary>
        /// Update the player data for the sender based on the data in the packet.
        /// </summary>
        /// <param name="sender">The sender of the packet.</param>
        public static void UpdateNetworkSession() {
            if (client != null) {
                NetIncomingMessage msg;
                while ((msg = client.ReadMessage()) != null) {
                    switch (msg.MessageType) {
                        case NetIncomingMessageType.DebugMessage:
                        case NetIncomingMessageType.ErrorMessage:
                        case NetIncomingMessageType.WarningMessage:
                        case NetIncomingMessageType.VerboseDebugMessage:
                            string text = msg.ReadString();
                            Console.WriteLine(text);
                            break;
                        case NetIncomingMessageType.DiscoveryResponse:
                            client.Connect(msg.SenderEndpoint);
                            break;
                        case NetIncomingMessageType.StatusChanged:
                            NetConnectionStatus status = (NetConnectionStatus)msg.ReadByte();
                            //else
                            //    s_form.DisableInput();
                            string reason = msg.ReadString();
                            Console.WriteLine(status.ToString() + ": " + reason);

                            if (status == NetConnectionStatus.Connected) {
                                NetOutgoingMessage om = client.CreateMessage();
                                // TODO : Ask network synchronisation
                                //om.Write((int)THObjects.THNetworkMessageType.Sync);
                                client.SendMessage(om, NetDeliveryMethod.ReliableOrdered);
                            }

                            break;
                        case NetIncomingMessageType.Data:
                            if (OnDataReceived != null)
                                OnDataReceived(msg, null);
                            break;
                    }
                }
            }
        }
    }
}
